home *** CD-ROM | disk | FTP | other *** search
- /*
- IC Specific Override.c
-
- This is a C port of the ICSpecificOverride.p file.
-
- History:
- 11/17/95 dhn - Started port.
- */
-
- /*
- Original comment:
-
- Internet Config Specific Overide Component
-
- Routine names have an ICSO prefix for Internet Config Specific Override.
-
- To create an IC override component you need to make a copy of this
- file and fill in the blanks. This is an N stage process:
-
- 1. Make a copy of this file.
- 2. Change kOurComponentManufacturer to your manufacturer code.
- 3. Add any shared globals to the sharedGlobals record.
- 4. If you have shared globals then init them in ICSOInitShared.
- 5. If the shared globals need cleaning up then clean them ICSOCleanShared.
- 6. Add any instance specific globals to globalsRecord.
- 7. If you have globals then init them in ICSOInitGlobals.
- 8. If the globals need cleaning up then clean them ICSOCleanGlobals.
- 9. If you want to add a completely new routine or remove support for one of the built in routines then modify
- ICSOCanDo accordingly.
- 10. Modify ICSOWhatToOverride to return the correct ProcPtr for each routine that you override or add.
- 11. Write each routine. If you want the component to continue calling through to the captured component for
- this routine then have your routine return delegateThisCallErr.
- 12. Smirk at the wonders of Component Manager.
- 13. Looking inside ICGenericOverride and frown at the wonders of Component Manager.
-
- Share and Enjoy.
-
- Quinn
- 12 Feb 1995
- */
-
- #include <Components.h>
- #include <Folders.h>
-
- #include "IC Component API.h"
- #include "IC Component Selectors.h"
-
- #include "IC Specific Override.h"
- #include "IC Generic Override.h"
-
- pascal short SneakyRandom(GlobalsHandle globals);
- pascal void SafeResolveAlias(FSSpec* fs);
- pascal OSErr GetNthTextFile(CInfoPBRec* cpbp,FSSpecPtr sig,short max_count,short* count);
- pascal void ChooseRandomSignature(GlobalsHandle globals);
- pascal ICError RSCBegin(GlobalsHandle globals,ICPerm perm);
- pascal ICError RSCGetPref(GlobalsHandle globals,StringPtr key,ICAttr* attr,Ptr buf,long* size);
- pascal ICError RSCSetPref(GlobalsHandle globals,StringPtr key,ICAttr* attr,Ptr buf,long* size);
-
- enum {
- uppICSOPrefProcInfo=kPascalStackBased
- | RESULT_SIZE(SIZE_CODE(sizeof(ComponentResult)))
- | STACK_ROUTINE_PARAMETER(1,SIZE_CODE(sizeof(GlobalsHandle)))
- | STACK_ROUTINE_PARAMETER(2,SIZE_CODE(sizeof(StringPtr)))
- | STACK_ROUTINE_PARAMETER(3,SIZE_CODE(sizeof(ICAttr*)))
- | STACK_ROUTINE_PARAMETER(4,SIZE_CODE(sizeof(Ptr)))
- | STACK_ROUTINE_PARAMETER(5,SIZE_CODE(sizeof(long*))),
- uppICSOBeginProcInfo=kPascalStackBased
- | RESULT_SIZE(SIZE_CODE(sizeof(ComponentResult)))
- | STACK_ROUTINE_PARAMETER(1,SIZE_CODE(sizeof(GlobalsHandle)))
- | STACK_ROUTINE_PARAMETER(2,SIZE_CODE(sizeof(ICPerm)))
- };
-
- pascal ComponentResult ICSOInitShared(GlobalsHandle globals){
-
- return noErr;
- }
-
- pascal ComponentResult ICSOCleanShared(GlobalsHandle globals){
-
- return noErr;
- }
-
- pascal ComponentResult ICSOInitGlobals(GlobalsHandle globals){
- ComponentResult err=noErr;
- short refnum,oldrefnum;
- StringHandle strh;
-
- (*globals)->random_seed=TickCount();
- (*globals)->current_signature=(Handle)0;
- (*globals)->default_signature=(Handle)0;
-
- oldrefnum=CurResFile();
- refnum=OpenComponentResFile((Component)(*globals)->self);
-
- if (refnum<=0)
- return resNotFound;
-
- UseResFile(refnum);
-
- strh=GetString(kRandomSigFoldName);
- if (strh==(StringHandle)0)
- err=resNotFound;
- else {
- BlockMoveData(*strh,(*globals)->sig_folder_name,(*strh)[0]+1);
- }
-
- if (err==noErr){
- (*globals)->default_signature=Get1Resource('TEXT',kDefaultSignature);
- if ((*globals)->default_signature==(Handle)0)
- err=resNotFound;
- else
- DetachResource((*globals)->default_signature);
- }
-
- CloseComponentResFile(refnum);
- UseResFile(oldrefnum);
-
- return err;
- }
-
- pascal ComponentResult ICSOCleanGlobals(GlobalsHandle globals){
- ComponentResult err=noErr;
-
- if ((*globals)->current_signature!=(Handle)0){
- DisposeHandle((*globals)->current_signature);
- (*globals)->current_signature=(Handle)0;
- }
- if ((*globals)->default_signature!=(Handle)0){
- DisposeHandle((*globals)->default_signature);
- (*globals)->default_signature=(Handle)0;
- }
-
- return err;
- }
-
- pascal ComponentResult ICSOCanDo(GlobalsHandle globals,short selector){
-
- return delegateThisCallErr;
- }
-
- pascal ComponentFunctionUPP ICSOWhatToOverride(GlobalsHandle globals,short selector){
- ComponentFunctionUPP proc=(ComponentFunctionUPP)0;
-
- if (selector==kICCGetPref)
- proc=BuildNewProc(RSCGetPref,uppICSOPrefProcInfo);
- else if (selector==kICCSetPref)
- proc=BuildNewProc(RSCSetPref,uppICSOPrefProcInfo);
- else if (selector==kICCBegin)
- proc=BuildNewProc(RSCBegin,uppICSOBeginProcInfo);
-
- return proc;
- }
-
- #include <QuickDraw.h>
-
- /*
- Get a random number without disturbing the random sequence in use by the application
- */
- pascal short SneakyRandom(GlobalsHandle globals){
- short ret,temp;
- QDGlobals* qdp;
- long a5;
-
- a5=SetCurrentA5();
-
- if (a5!=0L){
- // calc the top of the qd globals
- qdp=(QDGlobals*)(a5-(sizeof(QDGlobals)-sizeof(GrafPtr)));
-
- // save and reset the random seed
- temp=qdp->randSeed;
- qdp->randSeed=(*globals)->random_seed;
- }
-
- // get the random number
- ret=Random();
-
- if (a5!=0L){
- // restore the seed before returning
- qdp->randSeed=temp;
- }
-
- if (ret<0)
- return -ret;
-
- return ret;
- }
-
- pascal void SafeResolveAlias(FSSpec* fs){
- Boolean isFolder,wasAlias;
- FSSpec temp;
- long gv;
- OSErr oe;
-
- if ((Gestalt(gestaltAliasMgrAttr,&gv)==noErr)&&(gv&gestaltAliasMgrPresent)){
- BlockMoveData((Ptr)fs,(Ptr)(&temp),sizeof(FSSpec));
-
- oe=ResolveAliasFile(fs,true,&isFolder,&wasAlias);
-
- if (oe!=noErr)
- BlockMoveData((Ptr)(&temp),(Ptr)fs,sizeof(FSSpec));
- }
- }
-
- pascal OSErr GetNthTextFile(CInfoPBRec* cpbp,FSSpecPtr sig,short max_count,short* count){
- OSErr err;
- short index;
-
- *count=0;
- index=1;
-
- do {
- cpbp->hFileInfo.ioNamePtr=sig->name;
- cpbp->hFileInfo.ioDirID=sig->parID;
- cpbp->hFileInfo.ioVRefNum=sig->vRefNum;
- cpbp->hFileInfo.ioFDirIndex=index;
-
- err=PBGetCatInfoSync(cpbp);
-
- index++;
-
- if ((err==noErr)&&(!(cpbp->hFileInfo.ioFlAttrib & 16))&&(cpbp->hFileInfo.ioFlFndrInfo.fdType=='TEXT'))
- (*count)++;
- } while ((err==noErr)&&(*count<max_count));
-
- return err;
- }
-
- pascal void ChooseRandomSignature(GlobalsHandle globals){
- CInfoPBRec cpb;
- FSSpec sig;
- Handle texth=(Handle)0;
- OSErr err;
- short ref,count;
- long length;
- short junk;
-
- if ((*globals)->current_signature!=(Handle)0){
- DisposeHandle((*globals)->current_signature);
- (*globals)->current_signature=0;
- }
-
- BlockMoveData((Ptr)(*globals)->sig_folder_name,(Ptr)(sig.name),((*globals)->sig_folder_name)[0]+1);
-
- err=FindFolder(kOnSystemDisk,kPreferencesFolderType,kCreateFolder,&(sig.vRefNum),&(sig.parID));
- if (err==noErr){
- SafeResolveAlias(&sig);
- cpb.hFileInfo.ioNamePtr=sig.name;
- cpb.hFileInfo.ioVRefNum=sig.vRefNum;
- cpb.hFileInfo.ioDirID=sig.parID;
- cpb.hFileInfo.ioFDirIndex=0;
- err=PBGetCatInfoSync(&cpb);
- }
- if ((err==noErr)&&(!(cpb.hFileInfo.ioFlAttrib&16)))
- err=dirNFErr;
-
- if (err==noErr){
- sig.parID=cpb.hFileInfo.ioDirID;
- GetNthTextFile(&cpb,&sig,32767,&count);
- if (count==0)
- err=fnfErr;
- else {
- count=(SneakyRandom(globals)%count)+1;
- err=GetNthTextFile(&cpb,&sig,count,&junk);
- }
- }
-
- if (err==noErr){
- SafeResolveAlias(&sig);
- err=HOpen(sig.vRefNum,sig.parID,sig.name,fsRdPerm,&ref);
- }
-
- if (err==noErr){
- err=GetEOF(ref,&length);
- if (err==noErr){
- if (length>4096)
- length=4096;
- texth=NewHandle(length);
- err=MemError();
- }
- if (err==noErr)
- FSRead(ref,&length,*texth);
-
- FSClose(ref);
- }
-
- if (err!=noErr){
- DisposeHandle(texth);
- texth=(Handle)0;
- }
-
- if (texth==(Handle)0){
- texth=(*globals)->default_signature;
- err=HandToHand(&texth);
- if (err!=noErr)
- texth=(Handle)0;
- }
-
- (*globals)->current_signature=texth;
-
- }
-
- pascal ICError RSCBegin(GlobalsHandle globals,ICPerm perm){
- ChooseRandomSignature(globals);
- return delegateThisCallErr;
- }
-
- pascal ICError RSCGetPref(GlobalsHandle globals,StringPtr key,ICAttr* attr,Ptr buf,long* size){
- Str255 tmpstr;
- ICPerm perm;
- long max_size;
- ICError err;
-
- if (IUEqualString(key,kICSignature)==0){
- /*
- This is for compatibility with IC 1.0, which didn't call ICBegin/ICEnd through the target when
- it was done automagically because of a ICGet/SetPref call. So if there are no permissions then
- we know that we're in about to do an automagic ICBegin so we randomise the signature.
- */
-
- if ((ICCGetPerm((*globals)->delegate,&perm)==noErr)&&(perm==icNoPerm))
- ChooseRandomSignature(globals);
-
- max_size=*size;
-
- if ((*globals)->current_signature==(Handle)0)
- *size=0;
- else
- *size=GetHandleSize((*globals)->current_signature);
-
- err=noErr;
- if ((max_size<0)&&(buf!=(Ptr)0))
- err=paramErr;
-
- if ((err==noErr)&&(buf!=(Ptr)0)){
- if (*size>max_size)
- err=icTruncatedErr;
- else
- max_size=*size;
- if (max_size!=0)
- BlockMoveData((Ptr)(*((*globals)->current_signature)),buf,max_size);
- }
-
- *attr=ICattr_locked_mask + ICattr_volatile_mask;
- } else
- err=delegateThisCallErr;
-
- return err;
- }
-
- pascal ICError RSCSetPref(GlobalsHandle globals,StringPtr key,ICAttr* attr,Ptr buf,long* size){
- if (IUEqualString(key,kICSignature)==0)
- return icPermErr;
- return delegateThisCallErr;
- }
-